﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data.Common
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm
Imports System.Data.Objects.DataClasses
Imports System.Data.Objects
Imports System.Data
Imports System.Diagnostics
Imports System.Globalization
Imports System.Linq
Imports System.Runtime.CompilerServices
Imports ATS.Business.Entities


Friend Class AssetDataContext

    Inherits ObjectContext
    Public Const SettingsConnectionString As String = "name=Entities"
    Public Const ContainerName As String = "Entities"

#Region "Constructors"

    Public Sub New()
        MyBase.New(SettingsConnectionString, ContainerName)
        Initialize()
    End Sub

    Public Sub New(ByVal connectionString As String)
        MyBase.New(connectionString, ContainerName)
        Initialize()
    End Sub

    Public Sub New(ByVal connection As EntityConnection)
        MyBase.New(connection, ContainerName)
        Initialize()
    End Sub

    Private Sub Initialize()
        ' Creating proxies requires the use of the ProxyDataContractResolver and
        ' may allow lazy loading which can expand the loaded graph during serialization.
        ContextOptions.ProxyCreationEnabled = False
        AddHandler ObjectMaterialized, AddressOf HandleObjectMaterialized
    End Sub

    Private Sub HandleObjectMaterialized(ByVal sender As Object, ByVal e As ObjectMaterializedEventArgs)
        Dim entity As IObjectWithChangeTracker = TryCast(e.Entity, IObjectWithChangeTracker)
        If entity IsNot Nothing Then
            Dim changeTrackingEnabled As Boolean = entity.ChangeTracker.ChangeTrackingEnabled
            Try
                entity.MarkAsUnchanged()
            Finally
                entity.ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled
            End Try
            Me.StoreReferenceKeyValues(entity)
        End If
    End Sub

#End Region
#Region "ObjectSet Properties"

    Public ReadOnly Property MainAssetsTables() As ObjectSet(Of MainAssetsTable)
        Get
            If _mainAssetsTables Is Nothing Then
                _mainAssetsTables = CreateObjectSet(Of MainAssetsTable)("MainAssetsTables")
            End If
            Return _mainAssetsTables
        End Get
    End Property
    Private _mainAssetsTables As ObjectSet(Of MainAssetsTable)

    Public ReadOnly Property UserTables() As ObjectSet(Of UserTable)
        Get
            If _userTables Is Nothing Then
                _userTables = CreateObjectSet(Of UserTable)("UserTables")
            End If
            Return _userTables
        End Get
    End Property
    Private _userTables As ObjectSet(Of UserTable)

    Public ReadOnly Property admPasswordConfigs() As ObjectSet(Of admPasswordConfig)
        Get
            If _admPasswordConfigs Is Nothing Then
                _admPasswordConfigs = CreateObjectSet(Of admPasswordConfig)("admPasswordConfigs")
            End If
            Return _admPasswordConfigs
        End Get
    End Property
    Private _admPasswordConfigs As ObjectSet(Of admPasswordConfig)

    Public ReadOnly Property CompanyDetails() As ObjectSet(Of CompanyDetail)
        Get
            If _companyDetails Is Nothing Then
                _companyDetails = CreateObjectSet(Of CompanyDetail)("CompanyDetails")
            End If
            Return _companyDetails
        End Get
    End Property
    Private _companyDetails As ObjectSet(Of CompanyDetail)

    Public ReadOnly Property RFIDScanHistories() As ObjectSet(Of RFIDScanHistory)
        Get
            If _rFIDScanHistories Is Nothing Then
                _rFIDScanHistories = CreateObjectSet(Of RFIDScanHistory)("RFIDScanHistories")
            End If
            Return _rFIDScanHistories
        End Get
    End Property
    Private _rFIDScanHistories As ObjectSet(Of RFIDScanHistory)

    Public ReadOnly Property Categories() As ObjectSet(Of Category)
        Get
            If _categories Is Nothing Then
                _categories = CreateObjectSet(Of Category)("Categories")
            End If
            Return _categories
        End Get
    End Property
    Private _categories As ObjectSet(Of Category)

    Public ReadOnly Property Departments() As ObjectSet(Of Department)
        Get
            If _departments Is Nothing Then
                _departments = CreateObjectSet(Of Department)("Departments")
            End If
            Return _departments
        End Get
    End Property
    Private _departments As ObjectSet(Of Department)

    Public ReadOnly Property DisposedAssets() As ObjectSet(Of DisposedAsset)
        Get
            If _disposedAssets Is Nothing Then
                _disposedAssets = CreateObjectSet(Of DisposedAsset)("DisposedAssets")
            End If
            Return _disposedAssets
        End Get
    End Property
    Private _disposedAssets As ObjectSet(Of DisposedAsset)

    Public ReadOnly Property Images() As ObjectSet(Of Image)
        Get
            If _images Is Nothing Then
                _images = CreateObjectSet(Of Image)("Images")
            End If
            Return _images
        End Get
    End Property
    Private _images As ObjectSet(Of Image)

    Public ReadOnly Property LeasedAssets() As ObjectSet(Of LeasedAsset)
        Get
            If _leasedAssets Is Nothing Then
                _leasedAssets = CreateObjectSet(Of LeasedAsset)("LeasedAssets")
            End If
            Return _leasedAssets
        End Get
    End Property
    Private _leasedAssets As ObjectSet(Of LeasedAsset)

    Public ReadOnly Property Locations() As ObjectSet(Of Location)
        Get
            If _locations Is Nothing Then
                _locations = CreateObjectSet(Of Location)("Locations")
            End If
            Return _locations
        End Get
    End Property
    Private _locations As ObjectSet(Of Location)

    Public ReadOnly Property Persons() As ObjectSet(Of Person)
        Get
            If _persons Is Nothing Then
                _persons = CreateObjectSet(Of Person)("Persons")
            End If
            Return _persons
        End Get
    End Property
    Private _persons As ObjectSet(Of Person)

    Public ReadOnly Property Warranties() As ObjectSet(Of Warranty)
        Get
            If _warranties Is Nothing Then
                _warranties = CreateObjectSet(Of Warranty)("Warranties")
            End If
            Return _warranties
        End Get
    End Property
    Private _warranties As ObjectSet(Of Warranty)

    Public ReadOnly Property App_info() As ObjectSet(Of App_info)
        Get
            If _app_info Is Nothing Then
                _app_info = CreateObjectSet(Of App_info)("App_info")
            End If
            Return _app_info
        End Get
    End Property
    Private _app_info As ObjectSet(Of App_info)

#End Region
End Class
